package com.dog.basics.service.mtkj.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.mtkj.HospitalWheelchairAddDto;
import com.dog.basics.bean.dto.admin.mtkj.HospitalWheelchairEditDto;
import com.dog.basics.bean.dto.admin.mtkj.HospitalWheelchairFromDto;
import com.dog.basics.bean.model.mtkj.MtDeviceHospital;
import com.dog.basics.bean.model.mtkj.MtDeviceHospitalWheelchair;
import com.dog.basics.bean.model.mtkj.MtDeviceWheelchair;
import com.dog.basics.bean.vo.admin.mtkj.HospitalWheelchairVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.mtkj.MtDeviceHospitalMapper;
import com.dog.basics.dao.mtkj.MtDeviceHospitalWheelchairMapper;
import com.dog.basics.dao.mtkj.MtDeviceWheelchairMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.mtkj.IMtDeviceHospitalWheelchairService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 医院轮椅关联 服务实现类
 * </p>
 *
 * @author wph
 * @since 2020-09-29
 */
@Service
@AllArgsConstructor
public class MtDeviceHospitalWheelchairServiceImpl extends ServiceImpl<MtDeviceHospitalWheelchairMapper, MtDeviceHospitalWheelchair> implements IMtDeviceHospitalWheelchairService {
    private final MtDeviceHospitalMapper mtDeviceHospitalMapper;
    private final MtDeviceWheelchairMapper mtDeviceWheelchairMapper;

    /**
     * 分页的条件公用类
     *
     * @param name,mobile
     * @param hospitalIds
     */
    public List<Long> gethospitalIdsWhere(String name, List<Long> hospitalIds) {
        List<Long> collect = mtDeviceHospitalMapper.selectList(Wrappers.<MtDeviceHospital>lambdaQuery().like(StrUtil.isNotBlank(name), MtDeviceHospital::getHospitalName, name)).stream().map(e -> e.getId()).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)) {
            hospitalIds = collect;
        } else {
            hospitalIds.add(-2L);
        }
        return hospitalIds;
    }

    /**
     * 分页的条件公用类
     *
     * @param name,mobile
     * @param codeIds
     */
    public List<Long> getCodeIdsWhere(String name, List<Long> codeIds) {
        List<Long> collect = mtDeviceWheelchairMapper.selectList(Wrappers.<MtDeviceWheelchair>lambdaQuery().like(StrUtil.isNotBlank(name), MtDeviceWheelchair::getWheeCode, name)).stream().map(e -> e.getId()).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)) {
            codeIds = collect;
        } else {
            codeIds.add(-2L);
        }
        return codeIds;
    }

    /**
     * @Description: 分页医院轮椅关联
     * @Param: status
     * @return: IPage<HospitalWheelchairVo>
     * @Author: wph
     * @Date:
     */
    @Override
    public IPage<HospitalWheelchairVo> getPageList(PageParam page, HospitalWheelchairFromDto obj) {
        // 判断所属医院ID
        List<Long> hospitalIds = CollUtil.newArrayList();
        if (StrUtil.isNotBlank(obj.getHospitalName())) {
            hospitalIds = gethospitalIdsWhere(obj.getHospitalName(), hospitalIds);
        }
        // 判断所属轮椅编号ID
        List<Long> codeIds = CollUtil.newArrayList();
        if (StrUtil.isNotBlank(obj.getWheelchairCode())) {
            codeIds = getCodeIdsWhere(obj.getWheelchairCode(), codeIds);
        }
        IPage iPage = this.page(page, new LambdaQueryWrapper<MtDeviceHospitalWheelchair>()
                .in(CollUtil.isNotEmpty(hospitalIds), MtDeviceHospitalWheelchair::getHospitalId, hospitalIds)
                .in(CollUtil.isNotEmpty(codeIds), MtDeviceHospitalWheelchair::getWheelchairId, codeIds)
                .eq(ObjectUtil.isNotNull(obj.getHospitalId()), MtDeviceHospitalWheelchair::getHospitalId, obj.getHospitalId())
                .eq(ObjectUtil.isNotNull(obj.getWheelchairId()), MtDeviceHospitalWheelchair::getWheelchairId, obj.getWheelchairId())
                .orderBy(Boolean.TRUE, obj.getIsOrderByAvg(), MtDeviceHospitalWheelchair::getCreateTime));
        List<MtDeviceHospitalWheelchair> records = iPage.getRecords();
        iPage.setRecords(records.stream().map(e -> {
            HospitalWheelchairVo newsVo = new HospitalWheelchairVo();
            BeanUtil.copyProperties(e, newsVo);
            //查询
            MtDeviceHospital mtDeviceHospital = mtDeviceHospitalMapper.selectById(e.getHospitalId());
            MtDeviceWheelchair mtDeviceWheelchair = mtDeviceWheelchairMapper.selectById(e.getWheelchairId());
            newsVo.setHospitalName(ObjectUtil.isNotNull(mtDeviceHospital) ? mtDeviceHospital.getHospitalName() : "");
            newsVo.setWheelchairCode(ObjectUtil.isNotNull(mtDeviceWheelchair) ? mtDeviceWheelchair.getWheeCode() : "");
            return newsVo;
        }).collect(Collectors.toList()));
        return iPage;
    }

    /**
     * @Description: 增加医院轮椅关联
     * @Param: obj
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveObj(HospitalWheelchairAddDto obj) {
        MtDeviceHospital mtDeviceHospital = mtDeviceHospitalMapper.selectById(obj.getHospitalId());
        Validator.validateNotNull(mtDeviceHospital, "医院不存在!");
        MtDeviceWheelchair mtDeviceWheelchair = mtDeviceWheelchairMapper.selectById(obj.getWheelchairId());
        Validator.validateNotNull(mtDeviceWheelchair, "轮椅不存在!");
        //判断是否存在
        List<MtDeviceHospitalWheelchair> list = this.list(Wrappers.<MtDeviceHospitalWheelchair>lambdaQuery().eq(MtDeviceHospitalWheelchair::getWheelchairId, obj.getWheelchairId()).orderByDesc(MtDeviceHospitalWheelchair::getCreateTime));
        if (CollUtil.isNotEmpty(list)) {
            MtDeviceHospitalWheelchair dogNews = list.get(0);
            BeanUtil.copyProperties(obj, dogNews);
            dogNews.setUpdateTime(DateUtil.currentSeconds());
            dogNews.setUpdateBy(SecurityUtils.getSysUser().getUserId());
            Validator.validateTrue(this.updateById(dogNews), "修改数据失败!");
        } else {
            MtDeviceHospitalWheelchair dogNews = new MtDeviceHospitalWheelchair();
            BeanUtil.copyProperties(obj, dogNews);
            dogNews.setCreateTime(DateUtil.currentSeconds());
            dogNews.setCreateBy(SecurityUtils.getSysUser().getUserId());
            Validator.validateTrue(this.save(dogNews), "数据保存失败!");
        }
        mtDeviceWheelchair.setWheeState(1);
        mtDeviceWheelchair.setProvinceId(mtDeviceHospital.getProvinceId());
        mtDeviceWheelchair.setCityId(mtDeviceHospital.getCityId());
        mtDeviceWheelchair.setAreaId(mtDeviceHospital.getAreaId());
        mtDeviceWheelchair.setDetailedAddress(mtDeviceHospital.getDetailedAddress());
        Validator.validateTrue(retBool(mtDeviceWheelchairMapper.updateById(mtDeviceWheelchair)), "数据保存失败!");
        return Boolean.TRUE;
    }

    /**
     * @Description: 修改医院轮椅关联
     * @Param: obj
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editObj(HospitalWheelchairEditDto obj) {
        MtDeviceHospitalWheelchair dogNews = this.getById(obj.getId());
        Validator.validateNotNull(dogNews, "无此数据!");
        MtDeviceHospital mtDeviceHospital = mtDeviceHospitalMapper.selectById(obj.getHospitalId());
        Validator.validateNotNull(mtDeviceHospital, "医院不存在!");
        MtDeviceWheelchair mtDeviceWheelchair = mtDeviceWheelchairMapper.selectById(obj.getWheelchairId());
        Validator.validateNotNull(mtDeviceWheelchair, "轮椅不存在!");
        //判断是否存在
        List<MtDeviceHospitalWheelchair> list = this.list(Wrappers.<MtDeviceHospitalWheelchair>lambdaQuery().ne(MtDeviceHospitalWheelchair::getId, obj.getId()).eq(MtDeviceHospitalWheelchair::getHospitalId, obj.getHospitalId()).eq(MtDeviceHospitalWheelchair::getWheelchairId, obj.getWheelchairId()));
        Validator.validateFalse(CollUtil.isNotEmpty(list), "该轮椅已经属于医院!");
        List<MtDeviceHospitalWheelchair> list1 = this.list(Wrappers.<MtDeviceHospitalWheelchair>lambdaQuery().eq(MtDeviceHospitalWheelchair::getWheelchairId, obj.getWheelchairId()));
        Validator.validateFalse(CollUtil.isNotEmpty(list1), "该轮椅已经属于其他医院!");
        BeanUtil.copyProperties(obj, dogNews);
        dogNews.setUpdateTime(DateUtil.currentSeconds());
        dogNews.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        Validator.validateTrue(this.updateById(dogNews), "修改数据失败!");
        mtDeviceWheelchair.setWheeState(1);
        mtDeviceWheelchair.setProvinceId(mtDeviceHospital.getProvinceId());
        mtDeviceWheelchair.setCityId(mtDeviceHospital.getCityId());
        mtDeviceWheelchair.setAreaId(mtDeviceHospital.getAreaId());
        mtDeviceWheelchair.setDetailedAddress(mtDeviceHospital.getDetailedAddress());
        Validator.validateTrue(retBool(mtDeviceWheelchairMapper.updateById(mtDeviceWheelchair)), "数据保存失败!");
        return Boolean.TRUE;
    }

    /**
     * @Description: 删除医院轮椅关联
     * @Param: ids
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(Long id) {
        MtDeviceWheelchair mtDeviceWheelchair = mtDeviceWheelchairMapper.selectById(id);
        Validator.validateNotNull(mtDeviceWheelchair, "无此数据!");
        List<MtDeviceHospitalWheelchair> list = this.list(Wrappers.<MtDeviceHospitalWheelchair>lambdaQuery().eq(MtDeviceHospitalWheelchair::getWheelchairId, id).orderByDesc(MtDeviceHospitalWheelchair::getCreateTime));
        if (CollUtil.isNotEmpty(list)) {
            Validator.validateTrue(this.removeById(list.get(0).getId()), "数据删除失败!");
            mtDeviceWheelchair.setWheeState(0);
            mtDeviceWheelchair.setProvinceId(-1L);
            mtDeviceWheelchair.setCityId(-1L);
            mtDeviceWheelchair.setAreaId(-1L);
            Validator.validateTrue(retBool(mtDeviceWheelchairMapper.updateById(mtDeviceWheelchair)), "数据保存失败!");
        }
        return Boolean.TRUE;
    }
}
